package org.example.service.oaa;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.dao.dto.user.UserDto;
import org.example.skytool.security.EncodeUtil;
import org.example.utils.oaa.ExtendOaaService;
import org.example.skytool.wechat.login.WechatLoginUtil;
import org.example.dao.dao.user.*;
import org.example.dao.entity.user.*;
import org.example.exception.ParamLossErrorException;
import org.example.skytool.commom.IdWorker;
import org.example.skytool.commom.ParamUtil;
import org.example.skytool.security.JwtUtils;
import org.example.utils.oaa.RedisOaaService;
import org.example.utils.user.RedisUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 22449
 */
@Service("oaaService")
public class OaaServiceImpl {

    @Resource
    private UUserDao uUserDao;

    @Resource
    private ExtendOaaService extendOaaService;

    @Resource
    private IdWorker idWorker;

    @Resource
    private WechatLoginUtil wechatLoginUtil;

    @Resource
    private RedisUserService redisUserService;

    @Resource
    private UUserSpecialDao uUserSpecialDao;

    @Resource
    private RedisOaaService redisOaaService;

    public String login(String code) {
        //调用微信接口获取openId
        String openId = wechatLoginUtil.getOpenId(code);
        ParamUtil.StringParam(openId);
        //根据openId查询用户
        LambdaQueryWrapper<UUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UUser::getIsValid, 1);
        lambdaQueryWrapper.eq(UUser::getOpenId, openId);
        UUser user = uUserDao.selectOne(lambdaQueryWrapper);
        if (user == null) {
            //不存在 创建新用户
            user = extendOaaService.createUser(idWorker.nextId(), openId);
            uUserDao.insert(user);
        }
        String userId = user.getId().toString();
        String token = JwtUtils.getJwtToken(userId, user.getOpenId());
        redisUserService.setCacheToken(userId, token);
        UserDto userDto = uUserDao.selectUserDto(Long.valueOf(userId));
        redisUserService.setUserDto(userDto);
        redisUserService.setCachePermission(userDto);
        redisOaaService.setUserLoginLast(userId);
        return token;
    }

    public String verify(String token) {
        if (JwtUtils.validateJwtToken(token)) {
            Map<String, Object> map = JwtUtils.parseJwtToken(token);
            String userId = (String) map.get("userId");
            redisOaaService.setUserLoginLast(userId);
            String jwt = redisUserService.getCacheToken(userId);
            if (jwt.equals(token)) {
                return token;
            } else {
                return jwt;
            }
        } else {
            throw new ParamLossErrorException("传递的token有误，请重新登录");
        }
    }

    public String loginSpecial(String account,String password){
        password = EncodeUtil.encrypt(password);
        LambdaQueryWrapper<UUserSpecial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UUserSpecial::getAccount,account);
        lambdaQueryWrapper.eq(UUserSpecial::getPassword, password);
        UUserSpecial uUserSpecial = uUserSpecialDao.selectOne(lambdaQueryWrapper);
        if (uUserSpecial==null) {
            uUserSpecial = new UUserSpecial();
            uUserSpecial.setAccount(account);
            uUserSpecial.setPassword(password);
            uUserSpecial.setId(idWorker.nextId());
            uUserSpecialDao.insert(uUserSpecial);
        }
        String userId = uUserSpecial.getId().toString();
        String token = JwtUtils.getJwtToken(userId,"null");
        redisUserService.setCacheToken(userId, token);
        Set<String> role = new HashSet<>();
        role.add("用户");
        redisUserService.setCachePermission(userId,role,new HashSet<>());
        return token;
    }



}
